package com.mystudy.network;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * @program: infoalgorithm
 * @description: 简单的socket客户端
 * @author: zhouzhilong
 * @create: 2019-05-24 16:09
 **/
public class SimpleSocketClient {

    public static void main(String[] args) throws IOException {
//        Socket socket = null;
        InputStream input = null;
    OutputStream output = null;

    String serverIp = "127.0.0.1";
    int port = 10000;
    String [] data = {"First","Second","Third"};
        try(Socket socket = new Socket(serverIp,port)){
        output = socket.getOutputStream();

        input = socket.getInputStream();
        byte[]b = new byte[1024];
        for(int i = 0;i<5;i++){
            if(i <=2){

                output.write(data[i].getBytes());
                int n = input.read(b);
                System.out.println("服务器:"+new String(b,0,n));
            }else{
                if(i == 4){
                    output.write("啦啦啦".getBytes());

                }
            }
            int n = input.read(b);
            System.out.println("服务器:"+new String(b,0,n));
        }


    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        input.close();
        output.close();
    }
}


}
